package com.leo.business;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.leo.dao.AdminDao;
import com.leo.entity.Admin;
import com.leo.entity.User;
import com.leo.formbean.AdminForm;
import com.leo.util.InnerObjectUtile;
import com.leo.util.RegexUtil;
import com.leo.vo.Result;

@Transactional
public class AdminBusinessImp implements AdminBusiness {
	@Resource
	private AdminDao adminDao; 
	private RegexUtil regexUtil = new RegexUtil();
	
	@Override
	public Result regist(AdminForm adminForm){
		try {
			String username = adminForm.getUsername(), 
					password = adminForm.getPassword(), 
					passwordComfirm = adminForm.getPasswordComfirm();
			
			if (!regexUtil.userNameLegal(username, 4, 20)
					|| !regexUtil.userPasswordLegal(password, 6, 20) ){
				return new Result("failure", "字段不合法");
			}
			if(adminDao.findAdmin(username) != null){
				return new Result("failure", "用户名已存在");
			}
			Admin user = new Admin();
			user.setUsername(username);
			user.setPassword(password);
			adminDao.addAdmin(user);
		} catch (NullPointerException ex) {
			return new Result("failure", "字段不能为空");
		}

		return Result.success();
	}
	
	@Override
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public long getCount(boolean checkQueryRight, String searchKey, String keyType){
		return adminDao.getCount(checkQueryRight, searchKey, keyType);
	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public boolean isLogin(){
		return InnerObjectUtile.SESSION.getMap().containsKey("adminname");
	}
	
	@Override
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public Result Login(AdminForm adminForm){
		//check login
		Admin user = adminDao.findAdmin(adminForm.getUsername());
		if(user == null){
			return new Result("failure", "用户名不存在");
		}else if(  !adminForm.getPassword().equals( user.getPassword() ) ){
			return new Result("failure", "密码错误");
		}
		Map<String, String> session = InnerObjectUtile.SESSION.getMap();
		session.put("adminname",adminForm.getUsername());
		session.put("password", adminForm.getPassword());
		return new Result("success", "登录成功");
	}
	
	@Override
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public void logout(){
		InnerObjectUtile.SESSION.getMap().remove("adminname");
	}
	
	@Override
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public List listUsers(boolean checkQueryRight, String searchKey, String keyType,int pageIndex, int pageSize){
		return adminDao.listUsers(checkQueryRight, searchKey, keyType, pageIndex, pageSize);
	}
	
	@Override
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public User getUser(AdminForm adminForm){
		return adminDao.findUser(adminForm.getUsername());
	}
	
	@Override
	public Result userUpdate(User user){
		try {
			String username = user.getUsername(), 
					password = user.getPassword(),
					appURL = user.getAppURL(),
					queryRight = user.getQueryRight();
			
			if (!regexUtil.userNameLegal(username, 4, 20)
					|| !regexUtil.userPasswordLegal(password, 6, 20)
					|| !regexUtil.isHttpURL(appURL)) {
				return new Result("failure", "字段不合法");
			}
			User u = adminDao.findUser(username);
			if(u == null){
				return new Result("failure", "用户名不存在");
			}
			u.setAppURL(appURL);
			u.setUsername(username);
			u.setPassword(password);
			u.setQueryRight(queryRight);
			adminDao.updateUser(u);
		} catch (NullPointerException ex) {
			return new Result("failure", "字段不能为空");
		}
		return Result.success();
	}
	

	@Override
	public Result userAdd(User user){
		try {
			String username = user.getUsername(), 
					password = user.getPassword(),
					appURL = user.getAppURL(),
					queryRight = user.getQueryRight();
			
			if (!regexUtil.userNameLegal(username, 4, 20)
					|| !regexUtil.userPasswordLegal(password, 6, 20)
					|| !regexUtil.isHttpURL(appURL)) {
				return new Result("failure", "字段不合法");
			}
			User u = adminDao.findUser(username);
			if(u != null){
				return new Result("failure", "用户名已存在");
			}
			user.setOid(UUID.randomUUID().toString());
			adminDao.addUser(u);
		} catch (NullPointerException ex) {
			return new Result("failure", "字段不能为空");
		}
		return Result.success();
	}
	
	@Override
	public void deleteUsers(String usernameSet){
		adminDao.deleteUsers(usernameSet);
	}
}
